package com.htgd.gardenexpo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.htgd.gardenexpo.entity.GeBaseDepart;
import com.htgd.gardenexpo.dao.GeBaseDepartDao;
import com.htgd.gardenexpo.entity.PageQuery;
import com.htgd.gardenexpo.page.DepartQuery;
import com.htgd.gardenexpo.service.GeBaseDepartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 组织机构表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-01
 */
@Service
public class GeBaseDepartServiceImpl extends ServiceImpl<GeBaseDepartDao, GeBaseDepart> implements GeBaseDepartService {
    @Autowired
    private  GeBaseDepartDao geBaseDepartDao;

    @Override
    public void insert(GeBaseDepart geBaseDepart) {
        geBaseDepartDao.insert(geBaseDepart);
    }

    @Override
    public Page<GeBaseDepart> querylist(DepartQuery departQuery) {
        Page<GeBaseDepart> pageNew=new Page<>();
        Page<GeBaseDepart> p= Page.of(departQuery.getPageNum(), departQuery.getPageSize());
        Page<GeBaseDepart> page = lambdaQuery().page(p);
        //key,tittle
        for (GeBaseDepart record : page.getRecords()) {
            record.setKey(record.getId());
            record.setTitle(record.getDepartName());
            record.setValue(record.getId());
        }

        pageNew.setTotal(page.getRecords().size());
        //第0级
        pageNew.setRecords(Collections.singletonList(page.getRecords().get(0)));
        //第一级
        List<GeBaseDepart> geBaseDepartList1=new ArrayList<>();
        for (GeBaseDepart record : page.getRecords()) {
            if (record.getParentId().equals("10")){
                geBaseDepartList1.add(record);
            }
        }
        pageNew.getRecords().get(0).setChildren(geBaseDepartList1);
        //第二级
        for (GeBaseDepart child : pageNew.getRecords().get(0).getChildren()) {
            List<GeBaseDepart> geBaseDepartList2=new ArrayList<>();
            for (GeBaseDepart record : page.getRecords()) {
                if (record.getParentId().equals(child.getId())){
                    geBaseDepartList2.add(record);
                }
            }
            child.setChildren(geBaseDepartList2);
        }

        return pageNew;
    }

    @Override
    public void removeBefore() {
        geBaseDepartDao.removeBefore();
    }

    @Override
    public void insertBefore(GeBaseDepart geBaseDepart) {
        geBaseDepartDao.insertBefore(geBaseDepart);
    }

    @Override
    public List<GeBaseDepart> getBeforeList() {
        return geBaseDepartDao.getBeforeList();
    }

    @Override
    public GeBaseDepart getBeforeById(String id) {
        return geBaseDepartDao.getBeforeById(id);
    }
}
